<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="编号" prop="no" />
      <el-table-column align="center" label="模板名称" prop="title" />
      <el-table-column align="center" label="模板内容" prop="content" />
      <el-table-column align="center" label="创建时间" prop="createTime" />
      <el-table-column align="center" label="是否启用" prop="enable">
        <template slot-scope="scope">
          <el-tag :type="scope.row.enable == 0 ? 'success' : 'error' ">{{ scope.row.enable == 0 ? '启用' : '不启用' }}</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList"/>
  </div>
</template>
<style scoped>
.tit {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
<script>
import { listmail } from "@/api/comment";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination

export default {
  name: "Comment",
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 5,
        type: 2,
        sort: "add_time",
        order: "desc",
      },
      dataForm: {
        title: undefined,
        content: undefined,
        enable: 1
      },
      dialogStatus: '',
      textMap: {
        update: "编辑",
        create: "创建",
      },
      replyForm: {
        commentId: 0,
        content: "",
      },
      replyFormVisible: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
     handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          updateAd(this.dataForm)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v);
                  this.list.splice(index, 1, this.dataForm);
                  break;
                }
              }
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "更新成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response.data.errmsg,
              });
            });
        }
      });
    },
    getList() {
      this.listLoading = true;
        listmail(this.listQuery)
        .then((response) => {
          this.list = response.data.data.list;
          this.total = response.data.data.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.list = [];
          this.total = 0;
          this.listLoading = false;
        });
    },
  },
};
</script>
